package com.example.yuejianapp14.Activity.Fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.Activity.Clean;
import com.example.yuejianapp14.Activity.Activity.LoginActivity;
import com.example.yuejianapp14.Activity.Activity.UpdateUserActivity;
import com.example.yuejianapp14.Activity.Activity.UserCollectedActivity;
import com.example.yuejianapp14.Activity.Activity.UserEnterActivity;
import com.example.yuejianapp14.Activity.Activity.UserTieziActivity;
import com.example.yuejianapp14.Activity.JavaBean.User;
import com.example.yuejianapp14.Activity.Listener.ClubNoListListener;
import com.example.yuejianapp14.Activity.Model.UserModel;
import com.example.yuejianapp14.R;
import com.squareup.picasso.Picasso;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends Fragment {
    private View view;
    private Context mContext;
    private ImageView userinfo_pic;
    private TextView userinfo_username;
    private SharedPreferences sp;
private TextView session;

    public UserFragment() {
        // Required empty public constructor
    }
    ClubNoListListener<User> UserInfo_picListener=new ClubNoListListener<User>() {
        public void onResponse(User userInfo) {
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/User/"+userInfo.getUpicture()).into(userinfo_pic);
            userinfo_username.setText("用户名： "+userInfo.getUname());
        }
        @Override
        public void onFail(String msg) {
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_user, container, false);
        initRecyclerView();
        getData();
        try {
            session.setText(Clean.getTotalCacheSize(getActivity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ;
        return view;
    }
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayout userinfo_tiezi = (LinearLayout) getActivity().findViewById(R.id.userinfo_tiezi);
        LinearLayout userinfo_collected=(LinearLayout) getActivity().findViewById(R.id.userinfo_collected) ;
        LinearLayout userinfo_enter=(LinearLayout)  getActivity().findViewById(R.id.userinfo_enter) ;
        LinearLayout updateuser=(LinearLayout) getActivity().findViewById(R.id.updateuser) ;
         TextView clear2=(TextView) getActivity().findViewById(R.id.clear2) ;
        LinearLayout logoutlogin=(LinearLayout) getActivity().findViewById(R.id.logoutlogin) ;
        userinfo_tiezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserTieziActivity.class);
                startActivity(intent);
            }
        });
        userinfo_collected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserCollectedActivity.class);
                startActivity(intent);
            }
        });
        userinfo_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserEnterActivity.class);
                startActivity(intent);
            }
        });
        updateuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UpdateUserActivity.class);
                startActivity(intent);

            }
        });
        clear2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Clean.clearAllCache(getActivity());

                try {
                    session.setText(Clean.getTotalCacheSize(getActivity()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Toast.makeText(getActivity(), "清理缓存",Toast.LENGTH_SHORT).show();

            }
        });
        logoutlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
//      ??          sp = getActivity().getSharedPreferences("uid",0);
            }
        });

    }

    private void getData() {
        Intent intent = getActivity().getIntent();
        UserModel userModel=new UserModel();
        userModel.findUserinfo(sp.getString("uid",""),UserInfo_picListener);

    }

    private void initRecyclerView() {
        session=(TextView)view.findViewById(R.id.session);
        userinfo_pic=(ImageView) view.findViewById(R.id.userinfo_pic);
        userinfo_username=(TextView) view.findViewById(R.id.userinfo_username) ;
        sp = getActivity().getSharedPreferences("uid", Context.MODE_PRIVATE);

    }
    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

}
